﻿using _9jCardRegisterCollection.Utils;
using CardRegisterCollection.Model;
using CardRegisterCollection.Utility.Enum;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace _9jCardRegisterCollection.Views
{
    public partial class DomesticAttendeeEdit : UserControl
    {
        public DomesticAttendeeEdit(AntdUI.Window _window, CardRegisterCollection.Model.DomesticAttendee _domesticAttendee )
        {
             
            InitializeComponent();
            InitCmbText();
            domesticAttendee = _domesticAttendee;
            window = _window;
            txtWorkUnit.Text = domesticAttendee.WorkUnit;
            if(cmbCardType.Items.Contains(domesticAttendee.CardType))
                 cmbCardType.Text = domesticAttendee.CardType;
            txtGender.Text = domesticAttendee.Gender;
            txtIdNum.Text = domesticAttendee.IDNumber;
            txtName.Text = domesticAttendee.Name;
            txtGroup.Text = domesticAttendee.UserDefine1;
        }
        public bool SureToChange = false;
        private AntdUI.Window window;
        CardRegisterCollection.BLL.CardType cardTypeHelper = new CardRegisterCollection.BLL.CardType();
        private CardRegisterCollection.Model.DomesticAttendee domesticAttendee = new CardRegisterCollection.Model.DomesticAttendee();

        private void InitCmbText()
        {
            DataSet ds = cardTypeHelper.GetList("  type = '" + CardTypeEnum.境内人员证件 .ToString() + "' ");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                DataTable dt = ds.Tables[0];
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    cmbCardType.Items.Add(dt.Rows[i]["name"]);
                }
            }
        }
        private void button_ok_Click(object sender, EventArgs e)
        {
            SureToChange = false;
            if (!cmbCardType.Items.Contains(this.cmbCardType.Text))
            {
                AntdUI.Message.warn(window, "无该证卡类型", autoClose: 1);
                return;
            }
            if(Validation.CheckEmpty(window, this.txtName, "请输入人员姓名") && Validation.CheckEmpty(window, this.txtGender, "请输入性别") && Validation.CheckEmpty(window, this.txtIdNum, "请输入身份证号") && Validation.CheckEmpty(window, this.txtWorkUnit, "请输入工作单位") && Validation.CheckEmpty(window, this.cmbCardType, "请输入办证类型"))
            {
              
                domesticAttendee.Name = this.txtName.Text;
                domesticAttendee.Gender = this.txtGender.Text;
                domesticAttendee.IDNumber = this.txtIdNum.Text;
                domesticAttendee.CardType = this.cmbCardType.Text;
                domesticAttendee.WorkUnit = this.txtWorkUnit.Text;
                domesticAttendee.UserDefine1 = txtGroup.Text;
                //新增
                if (string.IsNullOrEmpty(domesticAttendee.Id))
                { 
                    domesticAttendee.Id = Guid.NewGuid().ToString();
                    domesticAttendee.CreatedBy = CurrentLoginInfo.UserInfo.UserName;
                    domesticAttendee.CreatedDate = DateTime.Now;
                    
                }
                else
                {
                    domesticAttendee.LastUpdatedBy = CurrentLoginInfo.UserInfo.UserName;
                    domesticAttendee.LastUpdatedDate = DateTime.Now;
                }
                SureToChange = true;
                this.Dispose();
            }
            
        }

        private void button_cancel_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }
    }
}
